var _that;
var _chooseCallBack;

function init(target, items, chooseCallBack) {
  _that = target;
  _chooseCallBack = chooseCallBack;
  _that.setData({
    cgr_show: false,
    cgritems: items,
    cur_cgr: 0
  });
}

function show() {
  if(!_that){
    return;
  }
  _that.setData({
    cgr_show: true,
  });

  _that.setData({
    showCgrCnt: true
  })
}

function close() {
  _that.setData({
    showCgrCnt: false
  })
  setTimeout(() => {
    _that.setData({
      cgr_show: false
    })
  }, 300)
}

function toggle() {
  if (!_that) return
  if (_that.data.cgr_show) {
    close()
  } else {
    show()
  }
}

function choose(e) {
  var cgrindex = e.currentTarget.dataset.cgrindex;
  _that.setData({
    cur_cgr: cgrindex
  });

  _chooseCallBack(_that.data.cgritems[cgrindex]);
}

module.exports = {
  init: init,
  show: show,
  close: close,
  toggle: toggle,
  choose: choose,
}
